package org.dq.like.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.dq.like.pojo.Result;
import org.dq.like.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 签到相关控制器
 *
 * @author klh
 * @date 2023/09/08 10:34:04
 */
@Slf4j
@RestController
@RequestMapping("/checkin")
public class CheckinController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 检查是否签到接口
     */
    private static final String checkCheckinUrl = "https://api.juejin.cn/growth_api/v2/get_today_status";

    /**
     * 签到接口
     */
    private static final String checkinUrl = "https://api.juejin.cn/growth_api/v1/check_in";

    /**
     * 免费抽奖接口
     */
    private static final String freeGiftUrl = "https://api.juejin.cn/growth_api/v1/lottery/draw";

    public String juejinCheckin = "juejinCheckin";

    /**
     * 检查是否已经签到
     *
     * @param getGift 是否免费抽奖
     * @return
     */
    @GetMapping("/check")
    public Result<?> check(@RequestParam(name = "getGift", defaultValue = "true") Boolean getGift) {
        if (redisUtil.hHasKey(juejinCheckin, JuejinLikeController.cookie1)) {
            return Result.OK("今日已签到!");
        }
        redisUtil.hset(juejinCheckin, JuejinLikeController.cookie1, 1);
        log.info("签到开始,早起的鸟儿有虫吃!");
        String giftMsg = "";
        boolean checkInDone = checkCheckin();
        if (checkInDone) {
            //已经签到过
            log.info("签到完成,今日已签到!");
            return Result.OK("今日已签到!");
        } else {
            //签到
            checkin();
            //免费抽奖
            giftMsg = freeGift();
            log.info("签到完成,giftMsg:{}", giftMsg);
            if (getGift) {
                return Result.OK("签到完成,免费抽奖奖励:" + giftMsg + "!");
            } else {
                return Result.OK("签到完成!");
            }
        }
    }

    /**
     * 检查是否已经签到
     */
    public boolean checkCheckin() {
        String url = checkCheckinUrl + "?" + JuejinLikeController.getSuffix2();
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(mediaType);
        List<String> cookies = new ArrayList<>();
        cookies.add(JuejinLikeController.cookie1);
        headers.put(HttpHeaders.COOKIE, cookies);
        headers.setOrigin("chrome-extension://lecdifefmmfjnjjinhaennhdlmcaeeeb");
        HttpEntity<String> request = new HttpEntity<String>(null, headers);
        ResponseEntity<String> resultEntity = restTemplate.postForEntity(url, request, String.class);
        if (resultEntity.hasBody()) {
            JSONObject result = JSONObject.parseObject(resultEntity.getBody());
            JSONObject data = result.getJSONObject("data");
            if (data != null && data.containsKey("check_in_done")) {
                return data.getBoolean("check_in_done");
            }
        }
        return false;
    }

    /**
     * 签到
     */
    public boolean checkin() {
        String url = checkinUrl;
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(mediaType);
        List<String> cookies = new ArrayList<>();
        cookies.add(JuejinLikeController.cookie1);
        headers.put(HttpHeaders.COOKIE, cookies);
        headers.setOrigin("chrome-extension://lecdifefmmfjnjjinhaennhdlmcaeeeb");
        HttpEntity<String> request = new HttpEntity<String>(null, headers);
        ResponseEntity<String> resultEntity = restTemplate.postForEntity(url, request, String.class);
        if (resultEntity.hasBody()) {
            JSONObject result = JSONObject.parseObject(resultEntity.getBody());
            JSONObject data = result.getJSONObject("data");
            if (data != null && data.containsKey("check_in_done")) {
                return data.getBoolean("check_in_done");
            }
        }
        return false;
    }

    /**
     * 免费抽奖
     */
    public String freeGift() {
        String url = freeGiftUrl + "?" + JuejinLikeController.getSuffix2() + "&msToken=ccwYbNs3OhCp4bJm72w_mLZ_d31lSP4FyfDR_qr37D37nBykQhiX0-xFDX_KKtsNq0A-Bx_wfVO9zWaW000m1MwIcpMbY5sKmJHPDhcyhtoeEMrfYNwV6PCyAvrEsVY%3D&a_bogus=O64x6cZOMsm1iwfeYXDz9amERXb0YW5-gZEPKsiBb0of";
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(mediaType);
        List<String> cookies = new ArrayList<>();
        cookies.add(JuejinLikeController.cookie1);
        headers.put(HttpHeaders.COOKIE, cookies);
        headers.setOrigin("chrome-extension://lecdifefmmfjnjjinhaennhdlmcaeeeb");
        HttpEntity<String> request = new HttpEntity<String>(null, headers);
        ResponseEntity<String> resultEntity = restTemplate.postForEntity(url, request, String.class);
        if (resultEntity.hasBody()) {
            JSONObject result = JSONObject.parseObject(resultEntity.getBody());
            JSONObject data = result.getJSONObject("data");
            if (data != null && data.containsKey("lottery_name")) {
                return data.getString("lottery_name");
            }
        }
        return "请求失败";
    }

}
